package com.ddgame.admob;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.backends.iosrobovm.IOSApplication;
import com.badlogic.gdx.backends.iosrobovm.IOSApplicationConfiguration;

import org.robovm.apple.coregraphics.CGSize;
import org.robovm.apple.foundation.NSArray;
import org.robovm.apple.foundation.NSBundle;
import org.robovm.apple.foundation.NSError;
import org.robovm.apple.foundation.NSURL;
import org.robovm.apple.foundation.NSUserDefaults;
import org.robovm.apple.gamekit.GKAchievement;
import org.robovm.apple.gamekit.GKScore;
import org.robovm.apple.uikit.UIApplication;
import org.robovm.apple.uikit.UIApplicationLaunchOptions;
import org.robovm.apple.uikit.UIScreen;

import java.util.ArrayList;

public abstract class IOSFather extends IOSApplication.Delegate {
    protected AdmobADManager mAdmobADManager;
    protected String mLanguage;
    protected String mAppID;
    protected boolean isNeedGameCenter = true;
    public IOSFather(){
        super();
    }

    @Override
    protected IOSApplication createApplication() {
        IOSApplicationConfiguration config = new IOSApplicationConfiguration();
        mAdmobADManager = AdmobADManager.getmInstance();
        mAppID = NSBundle.getMainBundle().getInfoDictionaryObject("CFBundleIdentifier").toString();
        mLanguage = NSUserDefaults.getStandardUserDefaults().getArray("AppleLanguages").get(0).toString();
        System.out.println("mAppID=" + mAppID + ", mLanguage=" + mLanguage);
        config.useAccelerometer = false;
        return new IOSApplication(getGameEntry(), config);
    }

    protected abstract ApplicationListener getGameEntry();
    protected abstract long getBestScore();

    protected void setNeedGameCenter(boolean flag){
        this.isNeedGameCenter = flag;
    }

    @Override
    public boolean didFinishLaunching(UIApplication application, UIApplicationLaunchOptions launchOptions) {
        Boolean b = super.didFinishLaunching(application, launchOptions);
        mAdmobADManager.initBanner();
        mAdmobADManager.initInterAd();
        mAdmobADManager.initRewardAd();
        if(isNeedGameCenter) {
            didFinishLaunching();
        }
        return b;
    }

    protected void showLeaderboards(){
        if (logState) {
            mGCManager.showLeaderboardsView(mAppID);
        } else {
            mGCManager.login();
        }
    }

    protected void gotoComment(){
        UIApplication.getSharedApplication().openURL(new NSURL(AdmobADManager.APP_URL), null, null);
    }

    protected void reportScroe(long best){
        System.out.println("------reportScroe best = " + best);
        if (logState) {
            mGCManager.reportScore(mAppID, best);
        } else {
            mGCManager.login();
        }
    }

    protected IOSGameCenter mGCManager;
    protected boolean logState = false;
    public void didFinishLaunching() {
        mGCManager = new IOSGameCenter(UIApplication.getSharedApplication().getKeyWindow(), new GameCenterListener() {
            @Override
            public void playerLoginFailed (NSError error) {
                System.out.println("playerLoginFailed. error: " + error);
                logState = false;
            }

            @Override
            public void playerLoginCompleted () {
                System.out.println("playerLoginCompleted");
                logState = true;
                long score = getBestScore();
                if(score > 0) {
                    mGCManager.reportScore(mAppID, score);
                }
            }

            @Override
            public void achievementReportCompleted () {
                System.out.println("achievementReportCompleted");
            }

            @Override
            public void achievementReportFailed (NSError error) {
                System.out.println("achievementReportFailed. error: " + error);
            }

            @Override
            public void achievementsLoadCompleted (ArrayList<GKAchievement> achievements) {
                System.out.println("achievementsLoadCompleted: " + achievements.size());
            }

            @Override
            public void achievementsLoadFailed (NSError error) {
                System.out.println("achievementsLoadFailed. error: " + error);
            }

            @Override
            public void achievementsResetCompleted () {
                System.out.println("achievementsResetCompleted");
            }

            @Override
            public void achievementsResetFailed (NSError error) {
                System.out.println("achievementsResetFailed. error: " + error);
            }

            @Override
            public void scoreReportCompleted () {
                System.out.println("scoreReportCompleted and successfully");
            }

            @Override
            public void scoreReportFailed (NSError error) {
                System.out.println("scoreReportFailed. error: " + error);
            }

            @Override
            public void leaderboardsLoadCompleted (NSArray<? extends GKScore> scores) {
                System.out.println("scoresLoadCompleted: " + scores.size());
            }

            @Override
            public void leaderboardsLoadFailed (NSError error) {
                System.out.println("scoresLoadFailed. error: " + error);
            }

            @Override
            public void leaderboardViewDismissed () {
                System.out.println("leaderboardViewDismissed");
            }

            @Override
            public void achievementViewDismissed () {
                System.out.println("achievementViewDismissed");
            }
        });
        mGCManager.login();
    }

    public static boolean isIphonex(){
        CGSize size = UIScreen.getMainScreen().getBounds().getSize();
        return (size.getWidth() / size.getHeight()) < 1200f / 2436f;
    }
}